package com.gitee.xhs.sampleRecod.transactionRecord.jmockTool;

import com.gitee.xhs.sampleRecod.transactionRecord.AbstractTransactionMockerInterceptor;
import com.gitee.xhs.sampleRecod.transactionRecord.Transaction;
import com.github.jsonzou.jmockdata.DataConfig;
import com.github.jsonzou.jmockdata.InterceptType;
import com.github.jsonzou.jmockdata.MockConfig;

import java.lang.reflect.Field;

/**
 * @author BigHead
 * @date 2022/7/4 16:52
 * @description TransactionLoopOneToFiveMockerInterceptor
 */
public class TransactionLoopOneToFiveMockerInterceptor extends AbstractTransactionMockerInterceptor {

    public static final Integer LOOP_KEY_FIVE = 5;
    private static final Double RANGE_DOUBLE = 500d;

    private static final String AMOUNT_KEY = "amount";

    @Override
    public Object mock(Class<Transaction> aClass, Field field, Transaction transaction, DataConfig dataConfig) throws IllegalAccessException {
        int remainder = getDataNum(null) % LOOP_KEY_FIVE;
        int group = getDataNum(null) / LOOP_KEY_FIVE;
        transaction.setAccountId(remainder + 1);
        transaction.setTimestamp(System.currentTimeMillis());
        switch (remainder) {
            case 0: {
                transaction0(field, transaction);
                break;
            }
            case 1: {
                transaction1(field, transaction);
                break;
            }
            case 2: {
                transaction2(group, field, transaction);
                break;
            }
            case 3: {
                transaction3(field, transaction);
                break;
            }
            case 4: {
                transaction4(field, transaction);
                break;
            }
            default:
                return InterceptType.MOCK;
        }
        return InterceptType.UNMOCK;
    }

    private void transaction0(Field field, Transaction transaction) {
        if (field.getName().equals(AMOUNT_KEY)) {
            transaction.setAmount(1.1);
        }
    }

    private void transaction1(Field field, Transaction transaction) {
        if (field.getName().equals(AMOUNT_KEY)) {
            transaction.setAmount(2.2);
        }
    }

    private void transaction2(int group, Field field, Transaction transaction) {
        if (field.getName().equals(AMOUNT_KEY)) {
            transaction.setAmount(group * RANGE_DOUBLE);
        }
    }

    private void transaction3(Field field, Transaction transaction) {
        if (field.getName().equals(AMOUNT_KEY)) {
            transaction.setAmount(0.00001);
        }
    }

    private void transaction4(Field field, Transaction transaction) {
        if (field.getName().equals(AMOUNT_KEY)) {
            transaction.setAmount(10000.00001);
        }
    }

    @Override
    public MockConfig getMockConfig() {
        MockConfig mockConfig = new MockConfig();
        mockConfig.registerBeanMockerInterceptor(Transaction.class, this);
        return mockConfig;
    }

}
